package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Cargo;
import com.nc.delivery_project.server.domain.Currency;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Natalie D.
* Date: 12/27/14
* Time: 6:02 PM
*/
public class CargoDao extends AbstractDao<Cargo> {

    protected final static String ENTITY_NAME = Cargo.class.getSimpleName();


    public CargoDao() {
        super(ENTITY_NAME);
    }
    public CargoDao(Connection connection) throws SQLException {
        super(connection, ENTITY_NAME);
    }



    @Override
    protected void prepareInsertStatement(CallableStatement statement, Cargo object) throws SQLException {
        statement.setInt(1, object.getOrderId());
        statement.setString(2, object.getTitle());
        statement.registerOutParameter(3, Types.NUMERIC);
        statement.setString(4, object.getTitle());
        statement.setInt(5, object.getWeight());
        statement.setInt(6, object.getHeight());
        statement.setInt(7, object.getWidth());
        statement.setInt(8, object.getDepth());
        statement.setInt(9, object.getCost());
        statement.setInt(10, object.getCurrency().getId());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Cargo object) throws SQLException {
        statement.setInt(1, object.getId());
        statement.setString(2, object.getTitle());
        statement.setInt(3, object.getWeight());
        statement.setInt(4, object.getHeight());
        statement.setInt(5, object.getWidth());
        statement.setInt(6, object.getDepth());
        statement.setInt(7, object.getCost());
//        statement.setString(8, object.getCurrency());
    }



    @Override
    protected List<Cargo> parseResultSet(ResultSet rs) throws SQLException
    {
        List<Cargo> cargos = new ArrayList<>();
        while (rs.next())
        {
            int cargoId=rs.getInt("CARGO_ID");
            String cargoName = rs.getString("NAME");
            int weight = rs.getInt("WEIGHT");
            int height = rs.getInt("HEIGHT");
            int width = rs.getInt("WIDTH");
            int depth = rs.getInt("DEPTH");
            int cost = rs.getInt("COSTV");
            Currency currency = new Currency(rs.getInt("CURRENCY"));
            Cargo c=new Cargo(cargoId,cargoName,cost,weight,height,width,depth,currency);
            cargos.add(c);
        }

        return cargos;
    }

    protected String getReadByOrderIdQuery() {
        return queryManager.getQuery(ENTITY_NAME, "select_by_order_id");
    }


    public List<Cargo> readByOrderId(int id)
    {
        try
        {
            PreparedStatement statement = connection.prepareStatement(getReadByOrderIdQuery());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            return parseResultSet(rs);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int create(Cargo obj) throws SQLException {
        return super.create(obj, 3);
    }

}
